﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Signage01.dao
{
    public class ScreenDAO
    {
        string serializationFile = "resources/screen/screen.bin";
        //private List<Timeline> listTimeline;
        static ScreenDAO screenDAO = null;

        private ScreenDAO()
        {

        }

        public static ScreenDAO getSingleTon()
        {
            if (screenDAO == null)
            {
                screenDAO = new ScreenDAO();
            }
            return screenDAO;
        }

        /*
        public int addScreen(Screen screen)
        {
            List<Screen> listScreen
        }
        */
        public bool screenExists(String screenName)
        {
            List<Screen> listScreen = getAll();

            foreach (Screen tempScreen in listScreen)
            {
                if (tempScreen.ScreenName == screenName)
                {
                    return true;
                }
            }
            return false;
        }

        public int deleteScreen(Screen screen)
        {
            List<Screen> listScreen = getAll(); 

            int index = 0;
            int deleteIndex = -1;

            foreach (Screen tempScreen in listScreen)
            {
                if (tempScreen.ScreenName == screen.ScreenName)
                {
                    deleteIndex = index;
                    break;
                }
                index++;
            }

            listScreen.RemoveAt(deleteIndex);
            saveListToFile(listScreen);
            return 1;
        }

        public int saveListToFile(List<Screen> listScreen)
        {
            try
            {
                // saving the list to the file
                using (Stream stream = File.Open(serializationFile, FileMode.OpenOrCreate))
                {
                    var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    bformatter.Serialize(stream, listScreen);
                    stream.Close();
                }
                return 1;
            }
            catch (Exception ex)
            {
                Console.WriteLine("save excepiton is:" + ex.ToString());
                return 0;
            }
        }

        public List<Screen> getAll()
        {
            // retrieving the list from the file
            try
            {
                using (Stream stream = File.Open(serializationFile, FileMode.OpenOrCreate))
                {
                    var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                    // if this line throws exception, the file is empty, so we have to create a new list in catch block
                    List<Screen> listTemp = (List<Screen>)bformatter.Deserialize(stream);
                    return listTemp;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("The file is empty");
                List<Screen> listTemp = new List<Screen>();
                return listTemp;
            }
        }
    }
}
